import SessionSellingServerUtil from '../../data/session/sessionSellingServer'
import SessionUtil from '../../data/session/session';
import SessionUserUtil from '../../data/session/sessionUser';
import SellingServerUtil from "../../data/sellingServer"
import InteractionUtil from '../../shell/ui/interaction';
const Common = require('../../utils/common.js');
Page({
    data: {
    },
    onLoad: function (options) {
        SessionUtil.getInstance().then(session => {
            let tipType = options.tipType, promoterId = options.promoterId, salesmanId = options.salesmanId, referer = options._r
            this.setData({
                token:session.token,
                referer: referer
            });
            wx.setNavigationBarTitle({
                title: '选择顾问'
            })
            console.log('consultant/consultant promoter id', promoterId, 'salesman id', salesmanId)
            InteractionUtil.showLoading(300)
            Promise.all([SellingServerUtil.getInstance(promoterId), salesmanId? SellingServerUtil.getInstance(salesmanId) : session.getTheSellingServer()])
                .then(servers => {
                    InteractionUtil.hideLoading()
                    let [promoterServer, lastServer] = servers
                    let renderData = {
                        titleSlogan: '选择酒水顾问，享受优质的购酒体验',
                        promoterVCard:{
                            avatarUri:promoterServer.getTheVCardSync().avatarUri,
                            showName:promoterServer.getTheVCardSync().name,
                            contactPhone:promoterServer.getTheVCardSync().contactPhone,
                            tags:promoterServer.getTheVCardSync().tags
                        },
                        greeting:'',
                        approve:{
                            text:'',
                            action:'',
                            serverId:0
                        },
                        reject:{
                            show:true,
                            text:'',
                            action: ''
                        }
                    }
                    switch (tipType) {
                        case 'no_selection':
                            renderData.greeting = `我是酒水顾问“${promoterServer.getTheVCardSync().name}”，很高兴为您服务!`;
                            renderData.approve = {
                                text:'好，就找您买酒',
                                action:`chooseNewServer`,
                                serverId: promoterServer.salesmanId
                            }
                            renderData.reject = {show:false}
                            break;
                        case 'with_selection':
                            renderData.greeting = `我是酒水顾问“${promoterServer.getTheVCardSync().name}”，我可以为您提供服务吗？`;
                            renderData.approve = {
                                text:'好，就找您买酒',
                                action:`chooseNewServer`,
                                serverId: promoterServer.salesmanId
                            }
                            renderData.reject = {
                                show: true,
                                text:`不，仍找“${lastServer.getTheVCardSync().name}”`,
                                action:`chooseLastServer`
                            }
                            break;
                        default:
                            // redirect index/index
                            wx.navigateBack()
                    }
                    this.setData(renderData)
                }).catch(()=>{
                    InteractionUtil.hideLoading()
                })
        })
    },
    /**不切换顾问 */
    chooseLastServer: function () {
        wx.switchTab({
            url: '/pages/index/index',
        })
    },

    /**分享--选择新顾问 */
    chooseNewServer:function(e){
        let newServerId = e.currentTarget.dataset.serverId
        InteractionUtil.showLoading(300)
        SessionSellingServerUtil.switchSellingServer(newServerId).then(message => {
            // 获取最新的顾问列表
            SellingServerUtil.getAccessibleServers(false, false)
            wx.setStorageSync("if_refresh", 2);
            return Promise.all([SessionSellingServerUtil.renewCache(), SessionUserUtil.renewCache()])
        }).then(()=>{
            console.log('select salesman page',this.data)
            InteractionUtil.hideLoading(1000)
            if(this.data.referer){
                let referer = decodeURIComponent(this.data.referer)
                if(referer.indexOf('/') !== 0) referer = '/' + referer;
                wx.redirectTo({
                    url: referer,
                    fail: () => {
                        // 出错后尝试切换tab
                        wx.switchTab({
                            url: referer,
                            fail: err=>{
                                console.log('跳转到来源页面失败',referer, err)
                                wx.switchTab({
                                    url: '/pages/index/index',
                                })
                            },
                        })
                    }
                });
            }else{
                wx.switchTab({
                    url: '/pages/index/index',
                })
            }
        }).catch(err=>{
            InteractionUtil.hideLoading()
            InteractionUtil.showAlert(err.message, '出错了！').then(res=>{
                return Common.showContips(this)
            }).then(()=>{
                wx.navigateBack();
            })
        })   
    },
})